package cn.xmy.demo01.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.*;

@WebServlet("/ReadHtmlServlet")
public class ReadHtmlServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //读取网页内容，写入文件
        String htmlContent = request.getParameter("content");
        String head="<%@ page contentType=\"text/html;charset=UTF-8\" language=\"java\" %>";
        String css1="<link href=\"css/web.a25c0247.css\" rel=\"stylesheet\">";
        String css2="<link href=\"css/web.a25c0247.css\" rel=\"stylesheet\">";
        String css3="<link href=\"css/web.a25c0247.css\" rel=\"stylesheet\">";
        File file1=new File("D:\\java666\\finalGenshin-project\\demo01\\web\\readTest.txt");
        String newContent=head+htmlContent;
        FileWriter fi=null;
        BufferedWriter bo=null;
        try {
            fi=new FileWriter(file1);
            bo=new BufferedWriter(fi);
            bo.write(newContent);
            bo.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bo.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}